library (Matrix)  
library (arules)

setwd("D:\\R\\datamining2\\")
###################################################
###a1	Temperature of patient { 35C-42C } 35.0-36.9 normal,37.0-37.9,low,38.0-39.9 middle,40.0-42.0 high	
###a2	Occurrence of nausea { yes, no }	
###a3	Lumbar pain { yes, no }	
###a4	Urine pushing (continuous need for urination) { yes, no }	
###a5	Micturition pains { yes, no }	
###a6	Burning of urethra, itch, swelling of urethra outlet { yes, no }	
###d1	decision: Inflammation of urinary bladder { yes, no }	
###d2	decision: Nephritis of renal pelvis origin { yes, no }
###################################################

###################################################
###Read Data
###################################################
diagnosis <- read.table('diagnosis.data',
                         header=F,
                         dec = ",",
                         sep='\t',
                         fileEncoding="UTF-16LE",
                         as.is=TRUE,
                         col.names=c('temperature','nausea','lumbar','urine','micturition','urethra','bladder','nephritis')
                        )
colum <- list('temperature','nausea','lumbar','urine','micturition','urethra','bladder','nephritis')
###################################################
###1 Preprocess,converted  dataset into forms that are suitable for Mining Association rules
###################################################
str_all=""
for(i in 1:nrow(diagnosis))
{
  if(as.numeric(diagnosis[i,1])<=36.9){str='normal'}
  else if(as.numeric(diagnosis[i,1])<=37.9){str='low'}
  else if(as.numeric(diagnosis[i,1])<=39.9){str='midde'}
  else if(as.numeric(diagnosis[i,1])<=42.0){str='high'}
 
  for(j in 2:ncol(diagnosis))
  {
    if(diagnosis[i,j]=="yes"){str <- paste(str,colum[[j]],sep=',')}
  }
  if(str_all==''){str_all=str}
  else{str_all <- paste(str_all,str,sep='\r')}
}
write(str_all,'data_preprocess.csv')
###################################################
###2 Find the frequent items 
###################################################
a <- read.transactions('data_preprocess.csv',format = 'basket',sep=',')
inspect(a) 
sink("frequent_items.txt")
frequentsets  = apriori (a, parameter = list (support=0.1,maxlen=8,target = "frequent itemsets"),control=list(sort=-1)) 
inspect(frequentsets) 
sink()
###################################################
###3 Export rules, calculate its support and confidence
###################################################
sink("rules.txt")
rules = apriori (a, parameter = list (maxlen=8,minlen=3,support=0.1,confidence=0.5), appearance= list(rhs=c('bladder','nephritis'),default="lhs"),control=list(sort=-1)) 
inspect(rules) 
sink()
###################################################
###4 Delete redundant rules X->Y ,Y!=bladder or nephritis
###################################################
subset.matrix<-is.subset(rules,rules)
subset.matrix[lower.tri(subset.matrix,diag=T)]<-NA
redundant<-colSums(subset.matrix,na.rm=T)>=1
rules.pruned<-rules[!redundant]

sink("rules_delete_redundant.txt")
inspect(rules.pruned) 
sink()
###################################################
###5 Evaluated rules, use the Lift
###################################################
sink("rules_delete_redundant_sorted_lift.txt")
rules.pruned.sorted_lift = sort(rules.pruned,by='lift')
inspect(rules.pruned.sorted_lift) 
sink()
###################################################
###6 Visualization, display rules
###################################################
library(grid)
library (arulesViz)

#Scatter chart
jpeg(file='Scatter.jpg' )
plot(rules) 
dev.off( )
jpeg(file='Scatter_delete_redundant.jpg' )
plot(rules.pruned.sorted_lift) 
dev.off( )
#Parallel coordinates
jpeg(file='Parallel.jpg' )
plot(rules,method="paracoord") 
dev.off( )
jpeg(file='Parallel_delete_redundant.jpg' )
plot(rules.pruned.sorted_lift, method="paracoord")
dev.off( )
#Bubble chart
jpeg(file='Bubble.jpg' )
plot(rules,method="grouped")
dev.off( )
jpeg(file='Bubble_delete_redundant.jpg' )
plot(rules.pruned.sorted_lift,method="grouped") 
dev.off( )
